<?php
    /**
    * @name 城市,地区类
    * @copyright tcsos inc
    * @version cooperation v1.0
    */
    class area{
        //xml缓存生成路径
        private $xml_path;
        
        //API接口地址
        private $api_path;
        
        //城市列表
        private $city_list = array();
        
        /**
        * 构造函数
        * 
        * @param string $xml_path
        * @param string $api_path
        * @return void
        */
        public function __construct($xml_path , $api_path){
            if($xml_path == '' || $api_path == ''){
                exit('class area __construct exit.');
            }
            $this->xml_path = $xml_path;
            $this->api_path = $api_path;
        }
        
        /**
        * 析构函数
        * 
        */
        public function __destruct(){

        }
        
        /**
        * 获取当前IP列表
        * 
        * @return array(array(id,cityname,country).....)
        */
        private function get_city_list(){

            if(!empty($this->city_list)){
                return $this->city_list;
            }
            $api_url = $this->api_path.'action_get_city_list?a=1';
            $value = dfopen($api_url);
            $this->city_list = $this->get_json_decode($value);
            if(!$this->city_list || empty($this->city_list)){
                return array();
            }
            return $this->city_list;
        }
        
        /**
        * 获取列表
        * 
        */
        public function get_area_list($default = '重庆'){
            $info = $this->get_ip_info(); //获取当前IP列表
            $city = $info[1] != '0' ? $info[1] : $default;
            return array('city'=>$city,'list'=>$this->get_city_list());
        }
        
        /**
        * 获取当前所在城市信息
        * 
        * @param string $name
        * @return array(id,cityname,country)
        */
        public function get_city_info($name){
            if(!$name){
                return false;
            }
            //获取列表
            $area_list = $this->get_city_list();
            foreach($area_list as $val){
                if($val[1] == $name){
                    $city = $val;
                    break;
                }
            }
            return $city;
        }
        
        /**
        * 获取ip的信息
        * 
        * @param string $ip
        */
        public function get_ip_info($ip = ''){
            if(!$ip){
                $ip = get_remote_ip();
            }
            $api_url = $this->api_path.'action_get_city?ip='.$ip;
            return $this->get_json_decode(dfopen($api_url));
        }
        
        /**
        * json
        *
        * @param string $result     
        * @return json
        */
        private function get_json_decode($result){
            if(!$result){
                return false;
            }
            $result = str_replace("];","]",$result);
            $result = trim(str_replace("var info = ","",$result));
            $result = str_replace("\t","",$result);
            $result = str_replace("\r","",$result);
            $result = str_replace("\n","",$result);
            //去除bom头
            $result = preg_replace( '/^(\xef\xbb\xbf)/', '', $result);
            $result = preg_replace( '/^(\xef\xbb\xbf)/', '', $result);
            return json_decode($result);
        }
 
        
    }
?>
